package rbacControllers

import (
	"github.com/astaxie/beego"
	"lazyops_go/models"
	"lazyops_go/tools"
)

type DictController struct {
	beego.Controller
}

//展示
func (c *DictController) IndexDict() {
	cur_page,_ := c.GetInt(":page",1)
	dict_list :=models.QueryDictAll()
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	data_list := models.QueryDictPage(cur_page)
	page:=tools.CreatePaging(cur_page,12,len(dict_list))
	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["dict_list"] = data_list
	c.Data["label"] = "配置字典"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/dictjs.html"
	c.TplName = "rbac/dict.html"
}


//添加
func (c *DictController) CreateDict() {
	key := c.GetString("key")
	val := c.GetString("val")
	msg := c.GetString("msg")

	dict := models.Dict{}
	dict.DictKey = key
	dict.DictVal = val
	dict.DictMsg = msg
	models.AddDict(dict)
	c.Data["json"] = "add success"
	c.ServeJSON()
}


//通过ID获取信息
func (c *DictController) GetDictById(){
	dict_id,_ := c.GetInt("dict_id",0)
	dict_obj := models.QueryDictOne(dict_id)
	c.Data["json"] = dict_obj
	c.ServeJSON()
}

//修改
func (c *DictController) EditDict()  {
	dict_id,_:= c.GetInt("dict_id", 0)
	key := c.GetString("key")
	val := c.GetString("val")
	msg := c.GetString("msg")

	//实例 Dict 结构体
	new_dict := models.Dict{}
	new_dict.DictKey = key
	new_dict.DictVal = val
	new_dict.DictMsg = msg
	models.UpdateDict(dict_id,new_dict)
	c.Data["json"] = "修改成功"
	c.ServeJSON()
}


//删除
func (c *DictController) DelDict() {
	dict_id,_:= c.GetInt("dict_id", 0)
	models.DeleteDict(dict_id)
	c.Data["json"] = "删除成功"
	c.ServeJSON()
}

//搜索
func (c *DictController) SearchDict() {
	key := c.Ctx.Input.Param(":key")
	dict_list := models.QueryDictSearch(key)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")

	page:=tools.CreatePaging(1,len(dict_list),len(dict_list))
	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["key"] = key
	c.Data["dict_list"] = dict_list
	c.Data["label"] = "配置字典"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/dictjs.html"
	c.TplName = "rbac/dict.html"
}
